﻿// GenerateMachineCode.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include <Windows.h>
#include <iostream>


char* cpu_id()
{
    INT32 dwBuf[4];
    __cpuidex(dwBuf, 1, 1);
    char szTmp[32] = { NULL };
    sprintf(szTmp, "%08X%08X", dwBuf[3], dwBuf[0]);
    return szTmp;
}

char* disk_id()
{
    try
    {
        char id[1024];
        wchar_t Name[MAX_PATH];
        DWORD serno;
        DWORD length;
        DWORD FileFlag;
        wchar_t FileName[MAX_PATH];
        BOOL Ret;
        Ret = GetVolumeInformation(L"C:\\", Name, MAX_PATH, &serno, &length, &FileFlag, FileName, MAX_PATH);
        if (Ret) sprintf(id, "%08X", serno);
        return id;
    }
    catch (...)
    {
        return (char*)"";
    }
}

extern "C" char* __stdcall computer_id()
{
    char cid[2048];
    strcpy(cid, cpu_id());
    //printf("cpuid=%s\n",cid);
    strcat(cid, disk_id());
    //printf("disk_id=%s\n",cid);
    return cid;
}

int main()
{
    std::cout << computer_id() << std::endl;
    system("pause");
}